<?php

/**
 * Created by JetBrains PhpStorm.
 * User: Đặng Khánh Trung
 * Date: 11/1/12
 * Time: 8:58 PM
 * To change this template use File | Settings | File Templates.
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
jimport('joomla.html.pagination');

/**
 * Quanlyphong Model
 */
class VastModelQuanlyphong extends JModelItem {

    /**
     * @var string phongs
     */
    private $_total = null;
    private $_pagination = null;
    protected $donvis;
    protected $phongs;

    public function __construct() {
        parent::__construct();

        $config = JVastConfig::getConfig();
        
        $this->setState('limit', (isset($config['numOfItemsPerPage']) ? (int)$config['numOfItemsPerPage'] : JRequest::getVar('limit', 5, '', 'int')));
        $this->setState('limitstart', JRequest::getVar('limitstart', 0, '', 'int'));

        $session = JFactory::getSession();

        $ten = JRequest::getString('phongTen', $session->get('quanlyphong.ten'));
        $donvi = JRequest::getInt('phongDonvi', $session->get('quanlyphong.donvi'));
                
        $session->set('quanlyphong.ten', $ten);
        $session->set('quanlyphong.donvi', $donvi);

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__phong AS a');

        if (!is_null($donvi) && $donvi != -1) {
            $query->where('a.donvi_id = ' . $donvi);
        }
            
        if (!is_null($ten) && $ten !== '') 
            $query->where('a.ten like \'%' . $ten . '%\'');
        elseif ($ten === '')
            $query->where('a.ten like \'%\'');

        $db->setQuery($query);
        $this->phongs = $db->loadObjectList();
        
        $this->_total = count($this->phongs);
    }

    /**
     * Get the message
     * @return string The message to be displayed to the user
     */
    public function getPhongs() {
        $session = JFactory::getSession();
        
        $ten = $session->get('quanlyphong.ten');
        $donvi = $session->get('quanlyphong.donvi');
        
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
                
        $limit = $this->getState('limit');
        $limitstart = $this->getState('limitstart');
        
        $query->select('a.*');
        $query->from('#__phong AS a');
        if ($donvi != -1)
        {
            $query->where('a.donvi_id = ' . $donvi);
        }
        if (!is_null($ten) && $ten !== '')
            $query->where('a.ten LIKE \'%' . $ten . '%\' LIMIT ' . $limitstart . ',' . $limit);
        elseif ($ten === '')
            $query->where('a.ten like \'%\' LIMIT ' . $limitstart . ',' . $limit);
            
        $db->setQuery($query);
        $this->phongs = $db->loadObjectList();

        return $this->phongs;
    }

    public function getPagination() {
        $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
        return $this->_pagination;
    }

    public function getTotal() {
        return $this->_total;
    }

    public function getDonvi()
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
                
        $query->select('a.*');
        $query->from('#__donvi AS a');

        $db->setQuery($query);
        $donvis = $db->loadObjectList();

        return $donvis;        
    }
}